package com.example.springboottest.example.clickhouse;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import ru.yandex.clickhouse.BalancedClickhouseDataSource;
import ru.yandex.clickhouse.settings.ClickHouseProperties;

import java.sql.*;
import java.util.Arrays;
import java.util.List;

/**
 * ClickHouse的底层访问接口支持TCP和HTTP两种协议，其中，
 * TCP协议拥有更好的性能，其默认端口为9000，主要用于集群间的内
 * 部通信及CLI客户端；而HTTP协议则拥有更好的兼容性，可以通过
 * REST服务的形式被广泛用于JAVA、Python等编程语言的客户端，其默
 * 认端口为8123。通常而言，并不建议用户直接使用底层接口访问
 * ClickHouse，更为推荐的方式是通过CLI和JDBC这些封装接口，因为
 * 它们更加简单易用。
 */
public class ClickHouseFeatureDemo {
    public static void main(String[] args) {
        create();
//        select();
    }

    /**
     * 随机插入1k条数据
     * 10个特征族，每个特征族有1~5种值
     */
    public static void create() {
        String sql = "INSERT INTO DB_TEST.feature_family_combine(id, feature_family_1, feature_family_2,feature_family_3,feature_family_4," +
                "feature_family_5,feature_family_6,feature_family_7,feature_family_8,feature_family_9" +
                ",feature_family_10) VALUES(?, ?, ?,?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement ps = singleDatasource().prepareStatement(sql);

            //索引从1开始
            for (int i = 1; i < 1001; i++) {
                ps.setString(1, IdUtil.simpleUUID());
                ps.setInt(2, RandomUtil.randomInt(1, 6));
                ps.setInt(3, RandomUtil.randomInt(1, 6));
                ps.setInt(4, RandomUtil.randomInt(1, 6));
                ps.setInt(5, RandomUtil.randomInt(1, 6));
                ps.setInt(6, RandomUtil.randomInt(1, 6));
                ps.setInt(7, RandomUtil.randomInt(1, 6));
                ps.setInt(8, RandomUtil.randomInt(1, 6));
                ps.setInt(9, RandomUtil.randomInt(1, 6));
                ps.setInt(10, RandomUtil.randomInt(1, 6));
                ps.setInt(11, RandomUtil.randomInt(1, 6));

                ps.addBatch();
                if (i % 1000 == 0) {
                    int[] num = ps.executeBatch();
                    System.out.println("共执行 " + num.length + " 条语句.");
                }
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void select() {
        String sql = "select * from DB_TEST.stu";
        try {
            ResultSet rs = singleDatasource().createStatement().executeQuery(sql);
            while (rs.next()) {
                // ResultSet 的下标值从 1 开始，不可使用 0，否则越界，报 ArrayIndexOutOfBoundsException 异常
                System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t"
                        + rs.getString(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static Connection singleDatasource() {
        Connection con = null;
        try {
            // 初始化驱动
            Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
            // url
            String url = "jdbc:clickhouse://localhost:8123/default";
            // 用户名密码
            String user = "default";
            String password = ""; // 登录
            con = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
}
